package handler

import (
	"github.com/gin-gonic/gin"
	"zy5/api/request"
	"zy5/internal/dao"
	"zy5/internal/model"
)

func CommentRemove(c *gin.Context) {
	var req request.CommentRemove
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "<UNK>",
			"data": nil,
		})
		return
	}
	var comment model.Comment
	if comment.UserId != c.GetUint("userId") {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "没有权限删除评论",
			"data": nil,
		})
		return
	}

	dao.GetOneByFields(&model.Comment{Id: req.Id}, &comment)
	if comment.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "当前评论不存在",
			"data": nil,
		})
		return
	}
	result := dao.Delete(&model.Comment{Id: comment.Id})
	if !result {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "删除失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
		"data": nil,
	})

}

func CommentList(c *gin.Context) {
	var req request.CommentList
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  err.Error(),
			"data": req,
		})
		return
	}
	var comments []model.Comment
	dao.GetListByFields(&model.Comment{
		Id:      req.Id,
		Content: req.Content,
		UserId:  req.UserId,
		ShopId:  req.ShopId,
	}, &comments)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": comments,
	})
}
